package com.sdkj.recruit.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.ILoginLogDao;
import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.model.LoginLog;
import com.sdkj.recruit.model.Paginate;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;

@Persistent
@Service("logDao")
public class LoginLogDao implements ILoginLogDao{

	@Autowired
	private IMongodbDAO dao;
	@Override
	public void registerLog(LoginLog log) {
		dao.saveOrUpdatePO(log);
	}
	@Override
	public long queryAllLogsCount(String companyId,Date begin,Date end) {
		return dao.getRowCount(LoginLog.class, QueryParamCreator.eq("companyId", companyId),QueryParamCreator.between("loginDate", begin, end));
	}
	@Override
	public long queryAllLogsCount(String companyId, Date end) {
		return dao.getRowCount(LoginLog.class, QueryParamCreator.eq("companyId", companyId),QueryParamCreator.le("loginDate", end));
	}
	@Override
	public List<LoginLog> queryLoginLogs(int pageNo, int pageSize, String companyId, Date begin, Date end) {
		
		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "loginDate");
		orders.add(order);

		List<Criteria> condList = new ArrayList<Criteria>();
		condList.add(QueryParamCreator.eq("companyId",companyId));
		condList.add(QueryParamCreator.between("loginDate", begin, end));
		return dao.getPOList(LoginLog.class, new Paginate(pageNo, pageSize), orders,
				(Criteria[]) condList.toArray(new Criteria[0]));
		
	}
	@Override
	public List<LoginLog> queryLoginLogs(int pageNo, int pageSize, String companyId, Date end) {
		List<Order> orders = new ArrayList<Order>();
		Order order = new Order(Direction.DESC, "loginDate");
		orders.add(order);

		List<Criteria> condList = new ArrayList<Criteria>();
		condList.add(QueryParamCreator.eq("companyId",companyId));
		condList.add(QueryParamCreator.le("loginDate", end));
		return dao.getPOList(LoginLog.class, new Paginate(pageNo, pageSize), orders,
				(Criteria[]) condList.toArray(new Criteria[0]));
	}

}
